/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Dimensions } from 'react-native';

enum WidthBreakpointType {
  WIDTH_XS = 320,
  WIDTH_SM = 600,
  WIDTH_MD = 840,
  WIDTH_LG = 1440,
  WIDTH_XL = Number.MAX_VALUE,
}

enum HeightBreakpointType {
  HEIGHT_SM = 0.8,
  HEIGHT_MD = 1.2,
  HEIGHT_LG = Number.MAX_VALUE,
}

export enum ScreenBreakpoint {
  XSMALL_LANDSCAPE = 0,
  XSMALL_SQUARE,
  XSMALL_PORTRAI,
  SMALL_LANDSCAPE,
  SMALL_SQUARE,
  SMALL_PORTRAI,
  MEDIUM_LANDSCAPE,
  MEDIUM_SQUARE,
  MEDIUM_PORTRAI,
  LARGE_LANDSCAPE,
  LARGE_SQUARE,
  LARGE_PORTRAI,
  XLARGE_LANDSCAPE,
  XLARGE_SQUARE,
  XLARGE_PORTRAI,
  LENGTH
}

// 根据屏幕宽度判断屏幕横向断点
function getWindowWidthBreakpoint(): number {
  let windoWidth = Dimensions.get('window').width;
  let values = Object.values(WidthBreakpointType).filter((v) => typeof v === 'number') as number[];
  let firstGreaterValue = values.find((v) => v >= windoWidth);
  if (firstGreaterValue !== undefined) {
    let index = values.indexOf(firstGreaterValue);
    return index;
  }
  return -1;
}


// 根据屏幕高度判断屏幕纵向断点
function getWindowHeightBreakpoint(): number {
  let windoHeight = Dimensions.get('window').height;
  let windoWidth = Dimensions.get('window').width;
  let aspectRatio = windoHeight / windoWidth;
  let values = Object.values(HeightBreakpointType).filter((v) => typeof v === 'number') as number[];
  let firstGreaterValue = values.find((v) => v >= aspectRatio);
  if (firstGreaterValue !== undefined) {
    let index = values.indexOf(firstGreaterValue);
    return index;
  }
  return -1;
}

// 根据屏幕横纵断点判断屏幕的断点区间
export function getScreenType(): ScreenBreakpoint {
  let windowWidthBreakpoint = getWindowWidthBreakpoint();
  let windowHeightBreakpoint = getWindowHeightBreakpoint();
  let screenTypeValue = windowWidthBreakpoint * 3 + windowHeightBreakpoint;
  if (screenTypeValue >= 0 || screenTypeValue < ScreenBreakpoint.LENGTH) {
    return screenTypeValue as ScreenBreakpoint;
  } else {
    return ScreenBreakpoint.XSMALL_LANDSCAPE;
  }
}

export { WidthBreakpointType, HeightBreakpointType };